use RabbitsWare

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

drop PROCEDURE [dbo].[TweetApolloRegisterCompany]
go

CREATE PROCEDURE [dbo].[TweetApolloRegisterCompany]
	@name varchar(128),
	@credits int,
	@userName varchar(128),
	@passwordHash varchar(40),
	@email varchar(128),
	@confirmed bit
as
begin

declare @avaiableId int

set @avaiableId = (select top 1 CompanyId from [TweetApolloCompany] order by CompanyId desc)+1

insert into [TweetApolloCompany]
(
	[CompanyId],
	[Name],
	[Credits],
	[UserName],
	[PasswordHash],
	[Email],
	[Confirmed]
)
values (@avaiableId, @name, @credits, @userName, @passwordHash, @email, 0)

return @avaiableId
end
GO

drop PROCEDURE [dbo].[TweetApolloIsUsernameAvaiable]
go

CREATE PROCEDURE [dbo].[TweetApolloIsUsernameAvaiable]
	@userName varchar(128)
as
begin

if exists (select * from [TweetApolloCompany] where [UserName] = @userName and [Confirmed] = 1)
	select 1
else
	select 0
end
GO

drop PROCEDURE [dbo].[TweetApolloLogin]
go

CREATE PROCEDURE [dbo].[TweetApolloLogin]
	@userName varchar(128),
	@passwordHash varchar(40)
as
begin

if exists (select * from [TweetApolloCompany]
where [UserName] = @userName and [PasswordHash] = @passwordHash and [Confirmed] = 1)
	select 1
else
	select 0

end
GO
